package cn.edu.scau.cmi.chenxiaoqin.client.total;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.edu.scau.cmi.chenxiaoqin.adapter.AdapterOfClass;
import cn.edu.scau.cmi.chenxiaoqin.adapter.AdapterOfObject;
import cn.edu.scau.cmi.chenxiaoqin.adapter.Student;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class AdapterClient extends Application{
	public void start(Stage primaryStage) {
		Pane pane=new Pane();
		VBox bigVBox=new VBox();
		HBox topHBox=new HBox(15);
		HBox bottomHBox=new HBox(50);
		
		
		ComboBox<String> wayComboBox = new ComboBox<String>();
		wayComboBox.getItems().addAll( "类适配器模式","对象适配器模式" );   
		wayComboBox.setValue("类适配器模式");
		
		TextField inputNumber=new TextField("在此输入学生学号");
		TextField inputName=new TextField("在此输入学生姓名");
		Button createButton=new Button("确定");
		topHBox.getChildren().addAll(wayComboBox,inputNumber,inputName,createButton);
		topHBox.setPadding(new Insets(15,15,15,15));
		
		VBox numberVBox=new VBox();
		VBox nameVBox=new VBox();
		List<Integer> numLabels=new ArrayList<Integer>();
		List<String> nameLabels=new ArrayList<String>();
		bottomHBox.getChildren().addAll(numberVBox,nameVBox);
		bottomHBox.setPadding(new Insets(15,15,15,15));
		bottomHBox.setAlignment(Pos.CENTER);
		numberVBox.setAlignment(Pos.TOP_CENTER);
		nameVBox.setAlignment(Pos.TOP_CENTER);
		Label n1=new Label("学号");
		Label n2=new Label("姓名");
		bottomHBox.setPrefSize(700,200);
		bottomHBox.setPrefHeight(250);
		ScrollPane sp = new ScrollPane();
		sp.setContent(bottomHBox);
		bigVBox.getChildren().addAll(topHBox,sp);
		
		wayComboBox.setOnAction(e->{
			numberVBox.getChildren().clear();
			nameVBox.getChildren().clear();
			primaryStage.setTitle(wayComboBox.getValue());
			numLabels.clear();
			nameLabels.clear();
		});
		createButton.setOnAction(e->{
			String numStr=inputNumber.getText();
			String name=inputName.getText();
			if(isNumeric(numStr)&&!name.equals("")) {
				int number=Integer.parseInt(numStr);
				String way=wayComboBox.getValue();
				List<Student> student = new ArrayList<Student>();
				
				if(!numLabels.isEmpty()) {
					for(int i=0;i<nameLabels.size();i++) {
						Student tem1=new Student();
						tem1.setNumber(numLabels.get(i));
						tem1.setName(nameLabels.get(i));
						student.add(tem1);
					}
					numberVBox.getChildren().clear();
					nameVBox.getChildren().clear();
				}
				Student tem2=new Student();
				tem2.setNumber(number);
				tem2.setName(name);
				numLabels.add(number);
				nameLabels.add(name);
				student.add(tem2);
				if(way.equals("类适配器模式")) {
					AdapterOfClass aoc=new AdapterOfClass();
					aoc.sortStudents(student);
				}
				else {
					AdapterOfObject aoo=new AdapterOfObject();
					aoo.sortStudents(student);
				}
				numberVBox.getChildren().add(n1);
				nameVBox.getChildren().add(n2);
				for(int i=0;i<student.size();i++) {
					String newNum=String.valueOf(student.get(i).getNumber());
					String newName=student.get(i).getName();
					Label numLabel=new Label(newNum);
					Label nameLabel=new Label(newName);
					numberVBox.getChildren().add(numLabel);
					nameVBox.getChildren().add(nameLabel);
				}
				
			}
			else {
				System.out.println("bus ");
			}
			
		});
		
		pane.getChildren().add(bigVBox);
		Scene scene =new Scene(pane,700,280);
		primaryStage.setTitle("类适配器模式");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	public boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        else if(str.equals("")) {
        	return false;
        }
        return true;
	}
}
